<template>
	<view class="mainpadding">
		<view class="sishi xiaobai fonweight margin_top6">欢迎登录</view>
		<view class="margin_top6 xiahuaxian">
			<input type="text" class="ziti" placeholder="请输入账号" v-model="mobile">
		</view>
		<view class="margin_top xiahuaxian">
			<input type="text" class="ziti" placeholder="请输入密码" v-model="password">
		</view>
		<view class="flexbetween margin_top" @click="mmtype=!mmtype">
			<view class="flexleft">
				<view class="weixz margin_right1" v-show="!mmtype"></view>
				<view class="yigoux margin_right1" v-show="mmtype"></view>
				<view class="xiaohei ershiba">记住密码</view>
			</view>
			<!-- <view class="xiaohei ershiba" @click="jumper('/pages/login/wangjimm')">忘记密码？</view> -->
		</view>
		<view class="flexleft margin_top6" @click="type=!type">
			<!-- 为选中 -->
			<view class="weixz margin_right1" v-show="!type"></view>
			<!-- 已选中 -->
			<view class="yigoux margin_right1" v-show="type"></view>
			<view class="flexleft">
				<view class="xiaohui ershiba">阅读并同意</view>
				<view class="xiaohui ershiba" @click.stop="jumper('/pages/system/system?type=1')">《用户协议》</view>
				<view class="xiaohui ershiba">和</view>
				<view class="xiaohui ershiba" @click.stop="jumper('/pages/system/system?type=2')">《隐私保护政策》</view>
			</view>
		</view>
		<view class="bigbtn margin_top4" @click="login()" v-if="!liandiantype">登录</view>
		<view class="bigbtn margin_top4" v-if="liandiantype">登录</view>
		<view class="flexcenter margin_top6">
			<view class="ershiba" style="color:#666666 ;">没有账号？</view>
			<view class="xiaohong ershiba" @click="jumper('/pages/login/zhuce')">立即注册</view>
		</view>
	</view>
</template>

<script>
	import WebSocket from '@/static/websocket.js' //websocket封装
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				mobile: "",
				type: false,
				mmtype:false,
				password: "",
				liandiantype: 0,
				websocket: null
			}
		},
		onLoad(){
			this.mobile = uni.getStorageSync("account") || ""
			this.password = uni.getStorageSync("password") || ""
		},
		methods: {
			linkWebsocket() {
				this.websocket = new WebSocket("wss://fun6688.fun:8383");
				this.websocket.connect()
				this.websocket.onMessage(result => {
					console.log('app.js收到服務器内容：' +result.data);
					uni.$emit('updatechatFn', "")
					uni.$emit('updatechatFnList', "")
				})
			},
			login(){
				let _this = this;
				if(_this.mobile == ''){
					httpRequest.toast("请输入手机号码!")
					return;
				}
				// if(!_this.isMobile(_this.mobile)){
				// 	httpRequest.toast("手机号码格式错误!")
				// 	return;
				// }
				if(this.password==""){
					httpRequest.toast("请输入密码")
					return false
				} 
				if(!this.type){
					httpRequest.toast("请认真阅读并勾选用户协议");
					return false;
				}
				this.liandiantype = 1
				uni.showLoading({
					mask:true,
					title:"请稍候"
				})
				httpRequest.request('/api/user/login','GET',{
					account:this.mobile, 
					password:this.password
				}).then(res => {
					if(res.code == 1){
						uni.hideLoading()
						httpRequest.toast('登录成功');
						uni.setStorageSync('token',res.data.userinfo.token);
						uni.setStorageSync('user_id',res.data.userinfo.user_id);
						uni.setStorageSync('mobile',this.mobile);
						this.linkWebsocket();
						if(this.mmtype){
							uni.setStorageSync("account",this.mobile)
							uni.setStorageSync("password",this.password)
						}
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}else{
						this.liandiantype = 0
						uni.hideLoading()
						httpRequest.toast(res.msg);
					}
				})
			},
			isMobile(value) {
				let a = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value); 
				return a;
			},
			jumper(url) {
				uni.navigateTo({
					url: url
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.ziti {
		font-size: 28rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		color: #999999;
	}
</style>